package servlet;


import bean.AdminBean;
import bean.CustomerBean;
import com.alibaba.fastjson.JSON;
import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class AdminHuntServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int aid;
        String name =request.getParameter("name");
        //处理中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        //获取传进来的aid和name
    try{
        aid = Integer.parseInt(request.getParameter("aid"));
        if(name==""){
            name="@";
        }
    }catch (Exception e){
        aid=0;
    }
        //获取模糊查询的数据
        String sql="SELECT * FROM admin where aid like  ? or  name like  ?";

        try {
            PreparedStatement ps = JdbcUtil.getConnection().prepareStatement(sql);
            ps.setInt(1,aid);
            ps.setString(2, "%"+name+"%");
            System.out.println(ps.toString());
            ResultSet set=ps.executeQuery();
            ArrayList<AdminBean> arr = new ArrayList<AdminBean>();
            while (set.next()){
                AdminBean ad = new AdminBean();
                ad.setAid(set.getInt(1));
                ad.setName(set.getString(2));
                ad.setSex(set.getString(3));
                ad.setAge(set.getInt(4));
                ad.setTel(set.getString(5));
                ad.setAddress(set.getString(6));
                ad.setWid(set.getInt(7));
                arr.add(ad);
            }
            String json = JSON.toJSONString(arr);
            out.print(json);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

